package keeto.com.vn.bms.ui.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import keeto.com.vn.bms.model.ListSurgeryModel;
import keeto.com.vn.bms.model.ObjectHeaderExpanModel;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.utils.Utils;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListExpanSurgeryAdapter extends BaseExpandableListAdapter{
	private LayoutInflater mInflater;
	private Context context;
	private ArrayList<ObjectHeaderExpanModel> listHeader;
	private HashMap<String ,ArrayList<ListSurgeryModel>> listContent;


	public ListExpanSurgeryAdapter(Context context,
			ArrayList<ObjectHeaderExpanModel> listHeader,
			HashMap<String, ArrayList<ListSurgeryModel>> listContent) {
		super();
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.listHeader = listHeader;
		this.listContent = listContent;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return listHeader.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return listContent.get(listHeader.get(groupPosition).getName()) == null ? 0 : listContent.get(listHeader.get(groupPosition).getName()).size();

	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return (ObjectHeaderExpanModel)listHeader.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return listContent.get(listHeader.get(groupPosition).getName()).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TextView tvheader;
		ImageView imgIndicator;
		convertView = mInflater.inflate(R.layout.item_header_expanlv_layout,null);
		tvheader = (TextView)convertView.findViewById(R.id.tvHeaderExpan);
		imgIndicator = (ImageView)convertView.findViewById(R.id.img_indicator);
		tvheader.setText(listHeader.get(groupPosition).getName());
		if (isExpanded){
			imgIndicator.setImageResource(R.drawable.icon_support_arraw_down);
		}else{
			imgIndicator.setImageResource(R.drawable.icon_support_arraw_right);

		}
		return convertView;
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupExpanded(groupPosition);
	}
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder holder;
		if (convertView == null){
			convertView = mInflater.inflate(R.layout.item_list_surgery_layout, null);
			holder = new ViewHolder();
			holder.rlPartent = (RelativeLayout)convertView.findViewById(R.id.rl_parent);
			holder.tv_item_surgery_name = (TextView)convertView.findViewById(R.id.tv_item_surgery_name);
			holder.tv_item_doctor_name = (TextView)convertView.findViewById(R.id.tv_item_doctor_name);
			holder.tv_item_hospital_name = (TextView)convertView.findViewById(R.id.tv_item_hospital_name);
			holder.tv_item_time = (TextView)convertView.findViewById(R.id.tv_item_time);
			holder.tv_item_status = (TextView)convertView.findViewById(R.id.tv_item_status);
			holder.ll_item_icon_status = (LinearLayout)convertView.findViewById(R.id.ll_item_icon_status);
			holder.tv_item_icon_status = (TextView)convertView.findViewById(R.id.tv_item_icon_status);
			holder.tv_item_icon_status.setVisibility(View.GONE);
			holder.imv_item_arrow = (ImageView)convertView.findViewById(R.id.imv_item_arrow);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		ListSurgeryModel model = (ListSurgeryModel) getChild(groupPosition, childPosition);
		holder.tv_item_surgery_name.setText(model.getSurgeryName());
		holder.tv_item_doctor_name.setText(model.getFullname());
		holder.tv_item_hospital_name.setText(model.getHospitalname());
		holder.tv_item_time.setText(Utils.convertTimeSurgery(model.getExecute_date()));
		holder.tv_item_status.setText(model.getSurgeryStatus());
		if (model.getSurgerySttId().equals("1")){
			holder.ll_item_icon_status.setBackgroundResource(R.drawable.icon_status_new);
			holder.tv_item_icon_status.setVisibility(View.VISIBLE);
			holder.tv_item_icon_status.setText(convertView.getResources().getString(R.string.tv_new));
			holder.imv_item_arrow.setVisibility(View.GONE);
			holder.rlPartent.setBackgroundColor(context.getResources().getColor(R.color.gray_hover));

		}else{
			holder.rlPartent.setBackgroundColor(Color.WHITE);
			holder.tv_item_icon_status.setVisibility(View.GONE);
			holder.imv_item_arrow.setVisibility(View.VISIBLE);
			holder.ll_item_icon_status.setBackgroundColor(Color.TRANSPARENT);
		}
		return convertView;
	}
	class ViewHolder{
		private RelativeLayout rlPartent;
		private TextView tv_item_surgery_name;
		private TextView tv_item_doctor_name;
		private TextView tv_item_hospital_name;
		private TextView tv_item_time;
		private TextView tv_item_status;
		private LinearLayout ll_item_icon_status;
		private TextView tv_item_icon_status;
		private ImageView imv_item_arrow;
	}
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
